﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Deep_Talk_Converter.ProcessPriorityUtils
{
    /// <summary>
    /// Utility class that will execute some clean up action when it
    /// goes out of scope. Usually best used in a "using" statement!
    /// Stolen from the web page:
    /// http://code.logos.com/blog/2008/08/leverage_using_blocks_with_scope.html
    /// </summary>
    public sealed class Scope : IDisposable
    {
        /// <summary>
        /// Factory for the scope object.
        /// </summary>
        /// <param name="fnDispose"></param>
        /// <returns></returns>
        public static Scope Create(Action fnDispose)
        {
            return new Scope(fnDispose);
        }

        public void Dispose()
        {
            if (m_fnDispose != null)
            {
                m_fnDispose();
                m_fnDispose = null;
            }
        }

        private Scope(Action fnDispose)
        {
            m_fnDispose = fnDispose;
        }
        Action m_fnDispose;

        /// <summary>
        /// SImple thign if you need some sort of default
        /// empty scope in some circumstances.
        /// </summary>
        public static readonly Scope Empty = new Scope(null); 

    }

}
